<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Models\Categories;
use App\Models\Channel;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Storage;

class ArticleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article());

        $grid->column('id', __('Id'));
        $grid->column('author', __('table.article.author'))->display(function ($value){
            return Administrator::find($value)->name;
        });
        $grid->column("thumbs",__('table.article.thumbs'))->image(Storage::disk("public")->url(''),50,50);

        $grid->column('categroy', __('table.article.category'))->display(function($id){
            return Channel::find($id)->title;
        });
        //$grid->column('content', __('Content'));
        $grid->column('hits', __('Hits'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->field('id', __('table.article.id'));
        $show->field('author', __('table.article.author'))->as(function ($id){
            return Administrator::find($id)->name;
        });
        //$show->field("thumbs",__('table.article.thumbs'));
        $show->field('categroy', __('table.article.categroy'))->as(
            function ($id){
                return Channel::find($id)->title;
            });
        $show->field('content', __('table.article.content'))->unescape();
        $show->field('hits', __('table.article.hits'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article());

        $form->select('author', '作者')->options(function($id){
            $user=Administrator::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/api/admin');
        $form->select('categroy', '分类')->options(
            Channel::selectOptions()
        );
        $form->image("thumbs",__('table.article.thumbs'));;
        $form->text('title',__('table.article.title'));
        $form->text('description',__('table.article.description'));

        #$form->files('audio','音频');
        $form->quill('content', __('内容'));
        $form->number('hits', __('热度'));

        return $form;
    }
}
